package com.example.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.result.Result;
import com.example.entity.mes.Maintain;
import com.example.service.MaintainService;
import com.example.mapper.MaintainMapper;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 14501
* @description 针对表【maintain】的数据库操作Service实现
* @createDate 2024-03-13 10:32:50
*/
@Service
public class MaintainServiceImpl extends ServiceImpl<MaintainMapper, Maintain> implements MaintainService{

    @Resource
    private MaintainMapper maintainMapper;

    @Override
    public Result queryMaintainIPageList(Integer current, Integer size, String code, String name, String type) {
       try{
           IPage<Maintain> page;
           IPage<Maintain> config = new Page<>(current, size);
           // 解决当搜索条件为空时，默认查询所有数据
           QueryWrapper<Maintain> maintainQueryWrapper = new QueryWrapper<>();
           if (code!=null&&code!=""){
               maintainQueryWrapper.like("code",code);
           }
           if (name!=null&&name!=""){
               maintainQueryWrapper.eq("name",name);
           }
           if (type!=null&&type!=""){
               maintainQueryWrapper.eq("type",type);
           }
           page = this.maintainMapper.selectPage(config, maintainQueryWrapper);
           // 每页展示的数据
           List<Maintain> staffDeptVOList = page.getRecords();
           // 将响应数据填充到map中
           if (page.getTotal()==0){
               return Result.error("暂无数据");
           }
           Map map = new HashMap();
           map.put("pages", page.getPages());
           map.put("total", page.getTotal());
           map.put("list", staffDeptVOList);
           return Result.success(map);
       }catch (Exception e) {
           return Result.error("分页查询异常" + String.valueOf(e),"500");
       }
    }

    @Override
    public Result queryMaintainById(Long id) {
        try{
            Maintain maintain = this.maintainMapper.selectById(id);
            if (maintain==null){
                return Result.error("未查询到数据","500");
            }
            return Result.success(maintain);
        }catch (Exception e){
            return Result.error("通过id进行查询发生异常"+String.valueOf(e),"500");
        }
    }

    @Override
    public Result insertMaintain(Maintain maintain) {
        try {
            int insert = this.maintainMapper.insert(maintain);
            if (insert>0){
                return Result.success("新增成功");
            }else {
                return Result.error("新增失败","500");
            }
        }catch (Exception e){
            return Result.error("插入发生异常"+String.valueOf(e),"500");
        }
    }
    @Override
    public Result updateMaintain(Maintain maintain) {
        try {
            int insert = this.maintainMapper.updateById(maintain);
            if (insert>0){
                return Result.success();
            }else {
                return Result.error("修改失败","500");
            }
        }catch (Exception e){
            return Result.error("修改发生异常"+String.valueOf(e),"500");
        }
    }

    @Override
    public Result deleteMaintain(Long id) {
       try {
           int i = this.maintainMapper.deleteById(id);
           if (i>0){
               return Result.success("删除成功");
           }else{
               return Result.error("删除失败");
           }
       }catch (Exception e){
           return Result.error("删除发生异常"+String.valueOf(e),"500");
       }
    }

    @Override
    @DS("mes")
    @Transactional(rollbackFor = Exception.class) // 批量执行需要加上事务,其中报错,进行回滚操作
    public Result deleteMaintainBatch(List<Integer> ids) {
        try {
            // 在多数据源下 此方法需要再Service实现类中的方法上面加上数据源区别注解(@DS（”xxx“）)
            if (removeByIds(ids)) {
                return Result.success("删除成功");
            }
            return Result.error("删除失败","500");

            // 不靠谱
//          int integer = 0 ;
//            for (Integer arr:ids) {
//                int i = this.maintainMapper.deleteById(arr);
//                if (i>0){
//                    integer++;
//                }
//            }
//            if (integer==ids.size()){
//                return Result.success("删除成功");
//            }
//            return Result.error("删除失败");
        }catch (Exception e){
            return Result.error("批量删除发生异常"+String.valueOf(e),"500");
        }
    }
}




